﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cofe.Core.Validater
{
    /// <summary>
    /// Indicate it's an sync or async action that returns a boolean.
    /// If multiple property providers in a property host support this actions, all actions will be executed, 
    /// and property host return true if ANY of the property provider's action return true.
    /// Support bool and int only, see PropertyPair + operator.
    /// </summary>
    public class AggregateResultAttribute : InvokeBehaviorAttribute
    {
        public AggregateResultAttribute()            
        {

        }
    }

    public class ReturnMaxOnlyAttribute : InvokeBehaviorAttribute
    {
    }

    public class ReturnMinOnlyAttribute : InvokeBehaviorAttribute
    {
    }


    /// <summary>
    /// Used by HandleOnesActionAttribute.
    /// </summary>
    public enum HandleOnceActionMode
    {
        /// <summary>
        /// Throw exception if not handled.
        /// </summary>
        Compulsory,
        /// <summary>
        /// Do not throw exception if not handled.
        /// </summary>
        Optional
    }

    /// <summary>
    /// Indicate if multiple property provider support this action, it will runs each of them one by one,
    /// until one that set the parameterDic["Handled"] to true. 
    /// Support IPropertyProvider only, see PropertyPair | operator.
    /// </summary>
    public class HandleOnesAttribute : InvokeBehaviorAttribute
    {

        public HandleOnesAttribute(HandleOnceActionMode handleMode)
            : base()
        {            
        }

        public HandleOnesAttribute()
            : this(HandleOnceActionMode.Compulsory)
        {
        }

        HandleOnceActionMode HandleMode { get; set; }


        public override ValidationResult ValidateActionResult(ParameterDic value, object result)
        {
            switch (HandleMode)
            {
                case HandleOnceActionMode.Compulsory :
                    if (!(value.IsHandled))
                        return new ValidationResult("The action is not handled.");
                    break;
                case HandleOnceActionMode.Optional :                    
                    break;
            }

            return ValidationResult.Success;
        }
    }

}
